{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 02\n",
    "\n",
    "# 逐项积\n",
    "Book_3《数学要素》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b30f4cb-779b-48b9-9205-457973d012ca",
   "metadata": {},
   "source": [
    "该代码使用`numpy`库计算了两个行向量和两个矩阵的逐元素乘积（element-wise product），即对同维度向量或矩阵的对应位置的元素逐一相乘，得到一个新的向量或矩阵。\n",
    "\n",
    "### 代码细节\n",
    "\n",
    "1. **行向量逐元素乘积**：\n",
    "   - 定义了两个行向量 $a = [1, 2, 3]$ 和 $b = [4, 5, 6]$。\n",
    "   - 计算$a$和$b$的逐元素乘积，逐元素乘积的公式为：\n",
    "\n",
    "     $$\n",
    "     a\\_times\\_b = [a[0] \\times b[0], a[1] \\times b[1], a[2] \\times b[2]] = [1 \\times 4, 2 \\times 5, 3 \\times 6] = [4, 10, 18]\n",
    "     $$\n",
    "\n",
    "   - 结果为向量$[4, 10, 18]$，表示两个行向量在各个位置上相乘的结果。\n",
    "\n",
    "2. **矩阵逐元素乘积**：\n",
    "   - 定义矩阵\n",
    "\n",
    "     $$\n",
    "     A = \\begin{bmatrix} 1 & 2 & 3 \\\\ 4 & 5 & 6 \\end{bmatrix}\n",
    "     $$\n",
    "\n",
    "     和矩阵\n",
    "\n",
    "     $$\n",
    "     B = \\begin{bmatrix} 1 & 2 & 3 \\\\ -1 & 0 & 1 \\end{bmatrix}\n",
    "     $$\n",
    "\n",
    "   - 逐元素乘积通过将矩阵$A$和$B$的对应元素分别相乘，得到新矩阵$A\\_times\\_B$：\n",
    "\n",
    "     $$\n",
    "     A\\_times\\_B = \\begin{bmatrix} A[0][0] \\times B[0][0] & A[0][1] \\times B[0][1] & A[0][2] \\times B[0][2] \\\\ A[1][0] \\times B[1][0] & A[1][1] \\times B[1][1] & A[1][2] \\times B[1][2] \\end{bmatrix} = \\begin{bmatrix} 1 \\times 1 & 2 \\times 2 & 3 \\times 3 \\\\ 4 \\times -1 & 5 \\times 0 & 6 \\times 1 \\end{bmatrix} = \\begin{bmatrix} 1 & 4 & 9 \\\\ -4 & 0 & 6 \\end{bmatrix}\n",
    "     $$\n",
    "\n",
    "   - 结果矩阵$A\\_times\\_B$为$\\begin{bmatrix} 1 & 4 & 9 \\\\ -4 & 0 & 6 \\end{bmatrix}$。\n",
    "\n",
    "### 总结\n",
    "\n",
    "此代码演示了行向量和矩阵的逐元素乘积计算。逐元素乘积广泛用于神经网络中的损失计算、矩阵处理、以及数据操作中的逐项相乘计算，是数值运算中常用的基本操作。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e31c80e8-2306-4a5e-8eb1-be2ec929bde7",
   "metadata": {},
   "source": [
    "## 行向量的逐元素乘积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f880cefe-d9c7-48d4-8b89-d1de27ba1876",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np  # 导入numpy库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "023a189b-873e-4087-a762-83a905c6a2dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1, 2, 3]])  # 定义行向量a\n",
    "a  # 输出行向量a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3973de60-41b6-487c-aff9-42413fc69446",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 5, 6]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([[4, 5, 6]])  # 定义行向量b\n",
    "b  # 输出行向量b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e1a47587-7f42-499d-8be1-7da3e5ad6f31",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4, 10, 18]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_times_b = a * b  # 计算行向量a和b的逐元素乘积\n",
    "a_times_b  # 输出行向量的逐元素乘积结果"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0293493a-7abd-4b3d-9a35-f78892dd6d7e",
   "metadata": {},
   "source": [
    "## 矩阵的逐元素乘积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ccb53aa5-25ee-47da-8b4f-915e41c8c355",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.array([[1, 2, 3],  # 定义矩阵A\n",
    "              [4, 5, 6]])\n",
    "A  # 输出矩阵A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a5ce2cbe-86a0-4417-a0dc-4b09b6fa9502",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3],\n",
       "       [-1,  0,  1]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B = np.array([[1, 2, 3],  # 定义矩阵B\n",
    "              [-1, 0, 1]])\n",
    "B  # 输出矩阵B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "57b8c65b-6b85-467c-ab23-4141d2fc06ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  4,  9],\n",
       "       [-4,  0,  6]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_times_B = A * B  # 计算矩阵A和B的逐元素乘积\n",
    "A_times_B  # 输出矩阵的逐元素乘积结果"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
